package shuiyuqing.network.http;

import com.alibaba.fastjson.JSON;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.xkh.xkh.R;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import com.xkh.xkh.MyApplication;
import com.nostra13.universalimageloader.core.ImageLoader;

import managers.VolleySingleton;
import shuiyuqing.fragments.WaterFragment;
import shuiyuqing.network.beans.IrrBMSystem;

/**
 * 获取此时刻前的照片信息
 * Created by 凯 on 2015/11/13.
 */
public class GetIrrSystemByTime {

    //
    Upload upload;
    Download download;

    public GetIrrSystemByTime(Delegate delegate, Upload upload) {
        this.delegate = delegate;
        this.upload = upload;
    }

    Delegate delegate;

    public Upload getUpload() {
        return upload;
    }

    public Download getDownload() {
        return download;
    }

    public Delegate getDelegate() {
        return delegate;
    }

    // 获取此前的照片
    public void get() {
        String IP = MyApplication.share().getResources().getString(R.string.IP);
        String url = "http://" + IP + "/guanqu/GetImageByTime";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        afterGet(response);
                        if (delegate != null) {
                            delegate.onResponse(response, GetIrrSystemByTime.this);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null) {
                    delegate.onErrorResponse(error, GetIrrSystemByTime.this);
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("data", JSON.toJSONString(upload));
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

    // 下载完成后
    private void afterGet(String response) {
        download = JSON.parseObject(response, Download.class);
        if (download.getStatus() == DownloadInfo.SUCCESS) {
            IrrBMSystem system = download.getIrrBMSystem();
            String IP = MyApplication.share().getResources().getString(R.string.serverIp);

            if (system != null && system.getFileName() != null) {
                String url = "http://" + IP + "/xingkaih/" + VolleySingleton.encodeUrl(system.getFileName());
                if (delegate != null) {
                    WaterFragment waterFragment = (WaterFragment) delegate;
//                    VolleySingleton.getVolleySingleton().displayImg(waterFragment.getImg_img(), url);
                    ImageLoader.getInstance().displayImage(url,waterFragment.getImg_img(),MyApplication.options);
                }
            }
        } else if (download.getStatus() == DownloadInfo.FAILED) {

        }
    }


    public static interface Delegate {
        public void onResponse(String response, GetIrrSystemByTime getIrrSystemByTime);

        public void onErrorResponse(VolleyError error, GetIrrSystemByTime getIrrSystemByTime);
    }


    // 上行数据
    public static class Upload {
        // 測站編碼
        String stadiaCode;

        // 上次的時間，是时间戳
        String time;

        public Upload(String stadiaCode, String time) {
            this.stadiaCode = stadiaCode;
            this.time = time;
        }

        public String getStadiaCode() {
            return stadiaCode;
        }

        public void setStadiaCode(String stadiaCode) {
            this.stadiaCode = stadiaCode;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

    }

    // 下行数据
    public static class Download {

        int status;
        // 最新照片
        IrrBMSystem irrBMSystem;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public IrrBMSystem getIrrBMSystem() {
            return irrBMSystem;
        }

        public void setIrrBMSystem(IrrBMSystem irrBMSystem) {
            this.irrBMSystem = irrBMSystem;
        }

    }
}
